var xmlhttp = null;


function OpenLarge (c, id) {
    window.open(c, id,
	      'width=800,height=520,scrollbars=yes,status=yes,toolbar=yes,resizable=yes');
}


function OpenPopup(c, id, left, top, width, height) {
	var openStr = 'width=' + width + ',height=' + height 
			+ ',scrollbars=yes,status=yes,toolbar=yes,resizable=yes';
    window.open(c, id, openStr);
}

function OpenMessageDialog(url, id, left, top, width, height) {
	var openStr = 'width=' + width + ',height=' + height 
			+ ',scrollbars=no,status=no,toolbar=no,resizable=no';
    window.open(url, id, openStr);
}

function changeColor(element, to) {
	//log('changeColor called');
	element.style.background = to;
};

function getXmlHttp() {
	return(xmlhttp);
}


function remoteCall(dest, callback, errorCallback) {
    log('remoteCall: ' + dest);

	if (window.ActiveXObject) {
	} else {
		try {
		    // FireFox permission
		    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		} catch (e) {
		    log("Exception granting Permission UniversalBrowserRead");
		}
	}


	try {
	   // Moz supports XMLHttpRequest. IE uses ActiveX.
	   // browser detction is bad. object detection works for any browser
	   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
			new ActiveXObject("Microsoft.XMLHTTP");

	} catch (e) {
	   // browser doesn't support ajax. handle however you want
	   alert("Browser error preparing web connection");
	}

	if (xmlhttp.overrideMimeType) {
	  xmlhttp.overrideMimeType('text/xml');
	}

	 // the xmlhttp object triggers an event everytime the status changes
	 // triggered() function handles the events
	 xmlhttp.onreadystatechange = callback;

	try {
		 // open takes in the HTTP method and url.
		 xmlhttp.open("GET", dest);
		 log("Request sent to server: " + dest);

		 // send the request. if this is a POST request we would have
		 // sent post variables: send("name=aleem&gender=male)
		 // Moz is fine with just send(); but
		 // IE expects a value here, hence we do send(null);
		 xmlhttp.send(null);
	} catch (e) {
		log('Exception sending request to server: ' + dest
	  	   + '   Error name: ' + e.name 
		   + '. Error message: ' + e.message);
		errorCallback(dest);
	}
}


// Post data
// postString: "name=aleem&gender=male"
function post(dest, postString, callback, errorCallback) {
    log('post() - dest ' + dest);
    log('post() - Post String: ' + postString);

	if (window.ActiveXObject) {
	} else {
		try {
		    // FireFox permission
		    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		} catch (e) {
		    log("Exception granting Permission UniversalBrowserRead");
		}
	}


	try {
	   // Moz supports XMLHttpRequest. IE uses ActiveX.
	   // browser detction is bad. object detection works for any browser
	   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
			new ActiveXObject("Microsoft.XMLHTTP");

	} catch (e) {
	   // browser doesn't support ajax. handle however you want
	   alert("Browser error preparing web connection");
	}

	if (xmlhttp.overrideMimeType) {
	  xmlhttp.overrideMimeType('text/xml');
	}

	 // the xmlhttp object triggers an event everytime the status changes
	 // triggered() function handles the events
	 xmlhttp.onreadystatechange = callback;

	try {
		 // open takes in the HTTP method and url.
		 xmlhttp.open("POST", dest);
		 log("Request sent to server: " + dest);
		 
		 //This indicatest that the postString will be in the form of parameters: param1=value1&param2=value2...
		 xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");

		 // sent post variables: send("name=aleem&gender=male)
		 // Moz is fine with just send(); but
		 // IE expects a value here, hence we do send(null);
		 log('post() - sending post request - Post String: ' + postString);
		 xmlhttp.send(postString);
	} catch (e) {
		log('Exception sending request to server: ' + dest
	  	   + '   Error name: ' + e.name 
		   + '. Error message: ' + e.message);
		errorCallback(dest);
	}
}




 var messageNumber = 1;
 function log(message) {
	element = document.getElementById("debugDiv");
	if (element != null)
		element.innerHTML = '<br><font class="subFont">#' + messageNumber + '&nbsp;' + message + '</font>' + element.innerHTML;
	messageNumber++;
 }
 
 function decodeLineFeeds(text) {
 	var after = text;
	var previousText = "";
	while (previousText != after) {
	    previousText = after;
	    textStr = new String(after);
		after = textStr.replace("[LF]", "\n");
	}

	previousText = "";
	while (previousText != after) {
	    previousText = after;
	    textStr = new String(after);
		after = textStr.replace("<br>", "\n");
	}
	return(after);
 } 

 function decodeLineFeedsHtml(text) {
 	var after = text;
	var previousText = "";
	while (previousText != after) {
	    previousText = after;
	    textStr = new String(after);
		after = textStr.replace("[LF]", "<br>");
	}
	return(after);
 } 
 
 function submitForm(formName) {
 	var form = 	element = document.getElementsByName(formName)[0];
 	form.submit();
 }
